package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersettingServiceImp implements OrdersettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;
    @Override
    public void add(List<OrderSetting> settingList) {
        if(settingList!=null&&settingList.size()>0){
            for (OrderSetting orderSetting : settingList) {
                long count  = ordersettingDao.findCountByTime(orderSetting);
                if(count!=0){
                    ordersettingDao.editNumberBYTime(orderSetting);
                }else{
                    ordersettingDao.insertOrdersetting(orderSetting);
                }
            }

        }

    }

    @Override
    public List<Map<String, Object>> find(String date) {
        String[] split = date.split("-");
        //数据库查询的数据
        List<OrderSetting> orderSettings = ordersettingDao.find(split[0],split[1]);
        //封装到前台的集合
        List<Map<String, Object>> maps = new ArrayList<>();
        HashMap<String, Object> hashMap=null;
        for (OrderSetting orderSetting : orderSettings) {
            hashMap=new HashMap<>();
            hashMap.put("date",orderSetting.getOrderDate().getDate());
            hashMap.put("number",orderSetting.getNumber());
            hashMap.put("reservations",orderSetting.getReservations());
            maps.add(hashMap);
        }
        return  maps;
    }

    @Override
    public void ediNumberByDate(OrderSetting orderSetting) {
        long countByTime = ordersettingDao.findCountByTime(orderSetting);
        if(countByTime!=0){
            ordersettingDao.editNumberBYTime(orderSetting);
        }else{
            ordersettingDao.insertOrdersetting(orderSetting);
        }
    }
}
